home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 8 / FM Towns Free Software Collection 8.iso / t_os / wink24h / src / xmodem2.c < prev   
Text File  |  1994-06-01  |  23KB  |  998 lines

  1.  
  2. #define SOH     '\x01'
  3. #define STX     '\x02'
  4. #define EOT     '\x04'
  5. #define ACK     '\x06'
  6. #define NAK     '\x15'
  7. #define CAN     '\x18'
  8. #define CRC     '\x43'
  9. #define    CR    '\x0D'
  10.  
  11. #define    XM_FILE        0
  12. #define    XM_FSIZE    2
  13. #define    XM_TBLK        3
  14. #define    XM_SSIZE    4
  15. #define    XM_SBLK        5
  16. #define    XM_RSIZE    6
  17. #define    XM_RBLK        7
  18. #define    XM_TMOVR    8
  19. #define    XM_RETRY    9
  20. /* ------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin --------------- */
  21.  
  22. #define    CPMEOF    '\x1A'
  23. #define YG      '\x47'
  24.  
  25. #define    XM_ERTIME    10
  26. #define    XM_RATE        11
  27. #define    XM_MSG        12
  28. /*****
  29. #define    XM_MSG        10
  30. *****/
  31. /* ---------------------------------------------------------------- */
  32.  
  33. #define    DSP_WAIT    50000
  34.  
  35. int    Recive()
  36. {
  37.     int      ch;
  38.     unsigned int st;
  39.     time_t   s,n;
  40.  
  41.     if ( RSB_Receive(port,&ch,&st) == 0 )
  42.     return (ch & 0xff);
  43.  
  44.     time(&s);
  45.     do {
  46.     if ( RSB_Receive(port,&ch,&st) == 0 )
  47.         return (ch & 0xff);
  48.     time(&n);
  49.     } while ( (n - s) < 5 );
  50.  
  51.     return ERR;
  52. }
  53. static int  Recive_str(arg,n)
  54. UCHAR    *arg;
  55. int    n;
  56. {
  57.     int        ch;
  58.  
  59.     while ( n-- > 0 ) {
  60.         if ( (ch = Recive()) == ERR )
  61.         return ERR;
  62.     *(arg++) = ch;
  63.     }
  64.     return FALSE;
  65. }
  66.  
  67. /*************************************************
  68.    <<< X-Modem UpLoad Abort Hit ESC Key >>>
  69.  
  70. File Name    A:WINK.EXP
  71. File Size    xxxx        Total Block  xxxx
  72. Send Size    xxxx        Send Block   xxxx
  73. Recive Size  xxxx        Recive Block xxxx
  74. Time Over    xxxx        ReTry Count  xxxx
  75. Message      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  76.  
  77. 012345678901234567890123456789012345678901234567890
  78. ***************************************************/
  79. void    Xmd_wind(md,file)
  80. char    *md,*file;
  81. {
  82.     int    i;
  83.     long   l;
  84.     char   tmp[160];
  85.     static char *menu[]={
  86.     "File Name",    "",
  87.     "File Size",    "Total Block",
  88.     "Send Size",    "Send Block",
  89.     "Recive Size",    "Recive Block",
  90.     "Time Over",    "ReTry Count",
  91. /* --------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin -------------- */
  92.     "Erapsed Time",    "Data Rate",
  93. /* ---------------------------------------------------------------- */
  94.     "Message",
  95.     NULL
  96.     };
  97.  
  98. /* --------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin --------------- */
  99.     Dmy_form(tmp,50,0x98,0x95,0x99); 
  100.     wrtstr(tmp,15,7,0x05);
  101.     Dmy_form(tmp,50,0x96,0x20,0x96); 
  102.     for ( i = 0 ; i < 10 ; i++ )
  103.         wrtstr(tmp,15,8+i,0x05);
  104.  
  105.     Dmy_form(tmp,50,0x9A,0x95,0x9B); 
  106.     wrtstr(tmp,15,17,0x05);
  107.  
  108.     sprintf(tmp,"<<< X/Y-Modem %s Abort Hit ESC Key >>>",md);
  109.     wrtstr(tmp,19,8,0x01);
  110. /*****
  111.     Dmy_form(tmp,50,0x98,0x95,0x99); 
  112.     wrtstr(tmp,15,7,0x05);
  113.  
  114.     Dmy_form(tmp,50,0x96,0x20,0x96); 
  115.     for ( i = 0 ; i < 9 ; i++ )
  116.         wrtstr(tmp,15,8+i,0x05);
  117.  
  118.     Dmy_form(tmp,50,0x9A,0x95,0x9B); 
  119.     wrtstr(tmp,15,17,0x05);
  120.  
  121.     sprintf(tmp,"<<< X-Modem %s Abort Hit ESC Key >>>",md);
  122.     wrtstr(tmp,20,8,0x01);
  123. *****/
  124. /* ---------------------------------------------------------------- */
  125.  
  126.     for ( i = 0 ; menu[i] != NULL ; i++ )
  127.     wrtstr(menu[i],17 + (i % 2) * 28,10 + i / 2,0x05);
  128.  
  129.     wrtstr(file,30,10,0x05);
  130. }
  131. void    Xmd_msg(msg)
  132. char    *msg;
  133. {
  134. /* -------------------- DATA RATE 92/02/17 Pumpkin ---------------- */
  135.     wrtstr(msg,30,16,0x05);
  136. /*****
  137.     wrtstr(msg,30,15,0x05);
  138. *****/
  139. /* ---------------------------------------------------------------- */
  140. }
  141. void    Xmd_val(no,val)
  142. int    no;
  143. long    val;
  144. {
  145.     char    tmp[40];
  146. /* -------------------- DATA RATE 92/02/17 Pumpkin ---------------- */
  147.     if (no == XM_ERTIME) {
  148.     sprintf(tmp,"%02ld:%02ld:%02ld",val/3600,(val%3600)/60,val%60);
  149.     } else {
  150.         sprintf(tmp,"%ld ",val);
  151.     }
  152. /*****
  153.     sprintf(tmp,"%ld",val);
  154. *****/
  155. /* ---------------------------------------------------------------- */
  156.     wrtstr(tmp,30 + (no % 2) * 28,10 + no / 2,0x05);
  157.  
  158. }
  159.  
  160. /* --------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin --------------- */
  161. int        crcmode=FALSE;
  162.  
  163. #define W    16    /* CRCビット数 */
  164. #define B    8     /* the number of bits per char */
  165. #define crctab crc_table
  166. extern unsigned short crctab[];
  167. /* ---------------------------------------------------------------- */
  168.  
  169. static int Send_blk(fp,bk,bufsize)
  170. FILE    *fp;
  171. int     bk;
  172. int    bufsize;
  173. {
  174.     int     i,j;
  175.     UCHAR   head[3];
  176.  
  177. /* --------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin -------------- */
  178.     unsigned short crcsum;
  179.     UCHAR   *p,tmp[1024];
  180.  
  181.     if (bufsize == 128) {
  182.         head[0]=SOH;
  183.     } else {
  184.         head[0]=STX;
  185.     }
  186.  
  187.     head[1]=bk;
  188.     head[2]=255-bk;
  189.  
  190.     if ( (i=fread(tmp,1,bufsize,fp)) <= 0 ) {
  191.     send_aux(EOT);
  192.         return ERR;
  193.     }
  194.  
  195.     for ( p=tmp+i ; i < bufsize ; i++ )
  196.         *(p++)='\0';
  197.  
  198.     for ( crcsum = j = i = 0 ; i < bufsize ; i++ ) {
  199.     if (crcmode==TRUE)  { /* CRC */
  200.             crcsum = (crcsum<<B) ^ crctab[(crcsum>>(W-B)) ^ tmp[i]];
  201.     } else {        /* checksum */
  202.          j += tmp[i]; j &= 0xff;
  203.     }
  204.     }
  205.  
  206.     send_str(head,3);
  207.     send_str(tmp,bufsize);
  208.     if (crcmode==TRUE)  { /* CRC */
  209.         send_aux(crcsum >> 8);
  210.         send_aux(crcsum & 255);
  211.     } else {        /* checksum */
  212.         send_aux(j);
  213.     }
  214.  
  215. /*******
  216.     UCHAR   *p,tmp[128];
  217.  
  218.     head[0]=SOH;
  219.     head[1]=bk;
  220.     head[2]=255-bk;
  221.  
  222.     if ( (i=fread(tmp,1,128,fp)) <= 0 ) {
  223.     send_aux(EOT);
  224.         return ERR;
  225.     }
  226.  
  227.     for ( p=tmp+i ; i < 128 ; i++ )
  228.         *(p++)='\0';
  229.     for ( j = i = 0 ; i < 128 ; i++,j &= 0xff )
  230.         j += tmp[i];
  231.  
  232.     send_str(head,3);
  233.     send_str(tmp,128);
  234.     send_aux(j);
  235. *******/
  236. /* ---------------------------------------------------------------- */
  237.  
  238.  
  239.     return FALSE;
  240. }
  241.  
  242.  
  243. /* --------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin --------------- */
  244. void    Up_Xmodem(file)
  245. char    *file;
  246. {
  247.     int     i,ch,bk,nk,ef;
  248.     unsigned int ec;
  249.     FILE    *fp;
  250.     long    size,remsize;
  251.     int        bufsize=128;
  252.     char    tmp[80];
  253.     time_t  fsttime,nowtime,*t=NULL;
  254.  
  255.     if (crcmode == TRUE) bufsize =1024;
  256.  
  257.     Xmd_wind("UpLoad",file);
  258.     if ( (fp = fopen(file,"rb")) == NULL ) {
  259.     Xmd_msg("File Not Found");
  260.     Soft_Timer(DSP_WAIT);
  261.     return;
  262.     }
  263.     fseek(fp,0L,2);
  264.     remsize = size = (long)ftell(fp);
  265.     fseek(fp,0L,0);
  266.     Xmd_val(XM_FSIZE,size);
  267.     i = size / bufsize;
  268.     i += ((size - i * bufsize)+127) / 128;
  269.     Xmd_val(XM_TBLK,i);
  270.     fsttime = time(t);
  271.  
  272.     for ( bk = 1, nk = 0, ef = FALSE, size = 0 ; ; ) {
  273.     if ( kbhit() != 0 ) {
  274.         ch = Get_key(&ec);
  275.         if ( ch == 0x1B || ec == 0x7200 ) {
  276.         Xmd_msg("Abort Xmodem Upload !!");
  277.             send_aux(CAN);
  278.         Soft_Timer(DSP_WAIT);
  279.         break;
  280.         }
  281.     }
  282.     for ( i = 0 ; i < 5 ; i++ ) {
  283.         if ( (ch = Recive()) != ERR ) 
  284.         break;
  285.         Xmd_val(XM_TMOVR,i);
  286.     }
  287.     if ( i >= 5 ) {
  288.         Xmd_msg("Time Over Aobrt !!");
  289.         Soft_Timer(DSP_WAIT);
  290.         break;
  291.     }
  292.     if ( ch == ACK) {
  293.         size += bufsize; remsize -= bufsize; nk = 0; bk++;
  294.  
  295.         if ( ef != FALSE ) {
  296.         Xmd_msg("End of Upload !!");
  297.         break;
  298.         }
  299.         if ( remsize < 0 && bufsize == 1024) {
  300.         remsize += bufsize;
  301.             bufsize = 128;
  302.         }
  303.  
  304.         if ( Send_blk(fp,bk,bufsize) != FALSE )
  305.         ef = TRUE;
  306.         else {
  307.         Xmd_val(XM_SSIZE,size + bufsize);
  308.         Xmd_val(XM_SBLK,bk);
  309.             nowtime = time(t);
  310.             if ((nowtime-fsttime) != 0) {
  311.                 Xmd_val(XM_RATE,(size+bufsize)/(nowtime-fsttime));
  312.             }
  313.         Xmd_val(XM_ERTIME,(nowtime-fsttime));
  314.         }
  315.     } else if ( ch == NAK  || ch == CRC ) {
  316.         if ( nk++ > 0 )
  317.         Xmd_val(XM_RETRY,nk);
  318.         fseek(fp,size,SEEK_SET);
  319.         if ( Send_blk(fp,bk,bufsize) != FALSE )
  320.         ef = TRUE;
  321.         else {
  322.         Xmd_val(XM_SSIZE,size + bufsize);
  323.         Xmd_val(XM_SBLK,bk);
  324.             nowtime = time(t);
  325.             if ((nowtime-fsttime) != 0) {
  326.                 Xmd_val(XM_RATE,(size+bufsize)/(nowtime-fsttime));
  327.             }
  328.         Xmd_val(XM_ERTIME,(nowtime-fsttime));
  329.         }
  330.     } else if ( ch == CAN ) {
  331.             Xmd_msg("Recive Cansel Code !!");
  332.         Soft_Timer(DSP_WAIT);
  333.             break;
  334.         }
  335.         if ( nk >= 5 ) {
  336.             Xmd_msg("ReTry Over Abort !!");
  337.         send_aux(CAN);
  338.         Soft_Timer(DSP_WAIT);
  339.         break;
  340.     }
  341.     }
  342.     fclose(fp);
  343. }
  344.  
  345. /**********
  346. void    Up_Xmodem(file)
  347. char    *file;
  348. {
  349.     int     i,ch,bk,nk,ef;
  350.     unsigned int ec;
  351.     FILE    *fp;
  352.     long    size;
  353.     char    tmp[80];
  354.  
  355.     Xmd_wind("UpLoad",file);
  356.     if ( (fp = fopen(file,"rb")) == NULL ) {
  357.     Xmd_msg("File Not Found");
  358.     Soft_Timer(DSP_WAIT);
  359.     return;
  360.     }
  361.     fseek(fp,0L,2);
  362.     size = (long)ftell(fp);
  363.     fseek(fp,0L,0);
  364.     Xmd_val(XM_FSIZE,size);
  365.     Xmd_val(XM_TBLK,(size + 127) / 128);
  366.  
  367.     for ( bk = 1, nk = 0, ef = FALSE, size = 0 ; ; ) {
  368.     if ( kbhit() != 0 ) {
  369.         ch = Get_key(&ec);
  370.         if ( ch == 0x1B || ec == 0x7200 ) {
  371.         Xmd_msg("Abort Xmodem Upload !!");
  372.             send_aux(CAN);
  373.         Soft_Timer(DSP_WAIT);
  374.         break;
  375.         }
  376.     }
  377.     for ( i = 0 ; i < 5 ; i++ ) {
  378.         if ( (ch = Recive()) != ERR ) 
  379.         break;
  380.         Xmd_val(XM_TMOVR,i);
  381.     }
  382.     if ( i >= 5 ) {
  383.         Xmd_msg("Time Over Aobrt !!");
  384.         Soft_Timer(DSP_WAIT);
  385.         break;
  386.     }
  387.     if ( ch == ACK ) {
  388.         size += 128; nk = 0; bk++;
  389.         if ( ef != FALSE ) {
  390.         Xmd_msg("End of Upload !!");
  391.         break;
  392.         }
  393.         if ( Send_blk(fp,bk) != FALSE )
  394.         ef = TRUE;
  395.         else {
  396.         Xmd_val(XM_SSIZE,size + 128);
  397.         Xmd_val(XM_SBLK,bk);
  398.         }
  399.     } else if ( ch == NAK ) {
  400.         if ( nk++ > 0 )
  401.         Xmd_val(XM_RETRY,nk);
  402.         fseek(fp,size,SEEK_SET);
  403.         if ( Send_blk(fp,bk) != FALSE )
  404.         ef = TRUE;
  405.         else {
  406.         Xmd_val(XM_SSIZE,size + 128);
  407.         Xmd_val(XM_SBLK,bk);
  408.         }
  409.     } else if ( ch == CAN ) {
  410.             Xmd_msg("Recive Cansel Code !!");
  411.         Soft_Timer(DSP_WAIT);
  412.             break;
  413.         }
  414.         if ( nk >= 5 ) {
  415.             Xmd_msg("ReTry Over Abort !!");
  416.         send_aux(CAN);
  417.         Soft_Timer(DSP_WAIT);
  418.         break;
  419.     }
  420.     }
  421.     fclose(fp);
  422. }
  423. *********/
  424. /* ---------------------------------------------------------------- */
  425.  
  426. void    Down_Xmodem(file)
  427. char    *file;
  428. {
  429.     int     i,j,ch,bk,nk,ef;
  430.     unsigned int ec;
  431.     long    size;
  432.     FILE    *fp;
  433. /* --------------XMODEM 1024/CRC 92/02/17 Pumpkin ----------------- */
  434.     time_t  fsttime,nowtime,*t=NULL;
  435.     int        bufsize = 128,k,sectnum,sectcur;
  436.     unsigned short crcsum;
  437.     UCHAR   tmp[1024];
  438. /**********
  439.     UCHAR   tmp[128];
  440. *********/
  441. /* ---------------------------------------------------------------- */
  442.  
  443.     Xmd_wind("DownLoad",file);
  444.     if ( (fp = fopen(file,"wb")) == NULL ) {
  445.     wrtstr("ファイルが作成出来ません",30,1,0x12);
  446.     return;
  447.     }
  448. /* ----------------- DATA RATE 92/02/17 Pumpkin ----------------- */
  449.     fsttime = time(t);
  450. /* ---------------------------------------------------------------- */
  451.     for ( ef = TRUE,bk = 1,size = 0,nk = 0 ; ; ) {
  452.     if ( kbhit() != 0 ) {
  453.         ch = Get_key(&ec);
  454.         if ( ch == 0x1B || ec == 0x7200 ) {
  455.         Xmd_msg("Abort Xmodem Download !!");
  456.             send_aux(CAN);
  457.         Soft_Timer(DSP_WAIT);
  458.         break;
  459.         }
  460.     }
  461.     for ( i = 0 ; i < 5 ; i++ ) {
  462. /* --------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin ----------------- */
  463.         if (crcmode==TRUE && (bk==1 || bk==2))  { /* CRC */
  464.         send_aux(CRC);
  465.         } else {
  466.             if ( ef == FALSE )
  467.             send_aux(ACK);
  468.             else if ( Flaing == FALSE )
  469.             send_aux(NAK);
  470.             else {
  471.             send_aux(NAK); send_aux(ACK);
  472.             }
  473.         }
  474. /**************
  475.         if ( ef == FALSE )
  476.         send_aux(ACK);
  477.         else if ( Flaing == FALSE )
  478.         send_aux(NAK);
  479.         else {
  480.         send_aux(NAK); send_aux(ACK);
  481.         }
  482. **************/
  483. /* ---------------------------------------------------------------- */
  484.         if ( (ch = Recive()) != ERR ) 
  485.         break;
  486.         Xmd_val(XM_TMOVR,i);
  487.     }
  488.  
  489.     if ( i >= 5 ) {
  490.         Xmd_msg("Time Over Abort !!");
  491.         Soft_Timer(DSP_WAIT);
  492.         break;
  493.     }
  494.  
  495.     if ( ch == EOT ) {
  496.         Xmd_msg("End of DownLoad !!");
  497.         if ( Flaing == FALSE )
  498.         send_aux(ACK);
  499.         break;
  500.     } else if ( ch == CAN ) {
  501.             Xmd_msg("Recive Cansel Code !!");
  502.         Soft_Timer(DSP_WAIT);
  503.             break;
  504. /* --------------XMODEM 1024/SUM/CRC 92/02/17 Pumpkin ----------------- */
  505.         } else if ( ch == SOH || ch == STX ) {
  506.         bufsize = (ch == SOH) ? 128 : 1024;
  507.         if (bk==1) {
  508.             if (ch == SOH && crcmode==TRUE) Xmd_msg("X-Modem 128/CRC");
  509.             if (ch == STX && crcmode==TRUE) Xmd_msg("X-Modem 1024/CRC");
  510.             if (ch == SOH && crcmode==FALSE) Xmd_msg("X-Mmodem 128/SUM");
  511.             if (ch == STX && crcmode==FALSE) Xmd_msg("X-Modem 1024/SUM");
  512.           }
  513.  
  514.         sectcur = Recive();
  515.             if ( (sectcur + Recive()) != 0xff ) {
  516.                Xmd_msg("PacNo. SUM Error");
  517.         goto ERROR;
  518.         }
  519.             if ( sectcur != (bk & 0xff) ) {
  520.         sprintf((char *)tmp,"PacNo. Err rcv=%4d cal=%4d",sectcur,bk);
  521.                Xmd_msg((char *)tmp);
  522.         goto ERROR;
  523.         }
  524.         if ( Recive_str(tmp,bufsize) != FALSE ) {
  525.                Xmd_msg("Data Read Error");
  526.         goto ERROR;
  527.         }
  528.         for ( crcsum = j = i = 0 ; i < bufsize ; i++ ) {
  529.         if (crcmode==TRUE)  { /* CRC */
  530.                 crcsum = (crcsum<<B) ^ crctab[(crcsum>>(W-B)) ^ tmp[i]];
  531.         } else {        /* checksum */
  532.              j += tmp[i]; j &= 0xff;
  533.         }
  534.         }
  535.  
  536.         if (crcmode==TRUE) { /* CRC */
  537.         k = Recive(); k = (k<<8) | Recive();
  538.             if ( k != crcsum ) {
  539.             sprintf((char *)tmp,"CRC Err rcv=%4X cal=%4X",k,crcsum);
  540.                     Xmd_msg((char *)tmp);
  541.             goto ERROR;
  542.         }
  543.         } else {
  544.             if ( Recive() != j ) {
  545.                     Xmd_msg("Check SUM Error");
  546.             goto ERROR;
  547.         }
  548.         }
  549.         
  550.  
  551.         fwrite(tmp,1,bufsize,fp);
  552.         size += bufsize;
  553.  
  554.         nowtime = time(t);
  555.         if ((nowtime-fsttime) != 0) {
  556.             Xmd_val(XM_RATE,size/(nowtime-fsttime));
  557.         }
  558.         Xmd_val(XM_ERTIME,(nowtime-fsttime));
  559.  
  560.         Xmd_val(XM_RSIZE,size);
  561.         Xmd_val(XM_RBLK,bk);
  562.         ef = FALSE;
  563.         nk = 0;
  564.         bk ++;
  565.         continue;
  566.     }
  567.  
  568.     sprintf((char *)tmp,"Undefined Code %2X     ",ch);
  569.     Xmd_msg(tmp);
  570.  
  571. ERROR:  Recive_str(tmp,bufsize); /* Dummy */
  572.  
  573. /**********
  574.         } else if ( ch == SOH ) {
  575.             if ( Recive() != (bk & 0xff) )
  576.         goto ERROR;
  577.             if ( (255 - Recive()) != (bk & 0xff) )
  578.         goto ERROR;
  579.         if ( Recive_str(tmp,128) != FALSE )
  580.         goto ERROR;
  581.         for ( j = i = 0 ; i < 128 ; i++,j &= 0xff )
  582.         j += tmp[i];
  583.         if ( Recive() != j )
  584.         goto ERROR;
  585.  
  586.         fwrite(tmp,1,128,fp);
  587.         size += 128;
  588.         Xmd_val(XM_RSIZE,size);
  589.         Xmd_val(XM_RBLK,bk);
  590.         ef = FALSE;
  591.         nk = 0;
  592.         bk++;
  593.         continue;
  594.     }
  595.  
  596. ERROR:  Recive_str(tmp,128);    /+ Dummy +/
  597. *********/
  598. /* ---------------------------------------------------------------- */
  599.  
  600.     ef = TRUE;
  601.     Xmd_val(XM_RETRY,++nk);
  602.         if ( nk >= 5 ) {
  603.             Xmd_msg("ReTry Over Abort !!");
  604.         send_aux(CAN);
  605.         Soft_Timer(DSP_WAIT);
  606.         break;
  607.     }
  608.     }
  609.     fclose(fp);
  610. }
  611.  
  612. void    Down_Ymodem()
  613. {
  614.     int     i,j,ch,bk,nk,ef;
  615.     unsigned int ec;
  616.     time_t  fsttime,nowtime,*t=NULL;
  617.     long    size, filesize, modtime, filemode;
  618.     FILE    *fp;
  619.     int        bufsize = 128,k,sectnum,sectcur;
  620.     unsigned short crcsum;
  621.     char    tmp[1024],file[128],*p,crc;
  622.  
  623.  
  624.     if ( Flaing == FALSE )  crc = CRC; else crc = YG;
  625.     Xmd_wind("DownLoad","");
  626.     for ( ef = TRUE,bk = 0,size = 0,nk = 0 ; ; ) {
  627.     if ( kbhit() != 0 ) {
  628.         ch = Get_key(&ec);
  629.         if ( ch == 0x1B || ec == 0x7200 ) {
  630.         Xmd_msg("Abort Ymodem Download !!");
  631.             send_aux(CAN);
  632.         Soft_Timer(DSP_WAIT);
  633.         break;
  634.         }
  635.     }
  636.     for ( i = 0 ; i < 5 ; i++ ) {
  637.         if (bk==0 || bk==1)  { /* CRC */
  638.         send_aux(crc);
  639.         } else {
  640.             if ( Flaing == FALSE ) {
  641.                 if ( ef == FALSE ) {
  642.                 send_aux(ACK);
  643.                 } else{
  644.                 send_aux(NAK);
  645.                 }
  646.             }
  647.         }
  648.         if ( (ch = Recive()) != ERR ) 
  649.         break;
  650.         Xmd_val(XM_TMOVR,i);
  651.     }
  652.  
  653.     if ( i >= 5 ) {
  654.         Xmd_msg("Time Over Aobrt !!");
  655.         Soft_Timer(DSP_WAIT);
  656.         break;
  657.     }
  658.  
  659.     if ( ch == EOT ) {
  660.         Xmd_msg("End of DownLoad !!");
  661.         send_aux(ACK); send_aux(crc);
  662.         if ( Flaing != FALSE ) {
  663.             ch = Recive();
  664.         bufsize = (ch == SOH) ? 128 : 1024;
  665.         Recive_str(tmp,bufsize+4); /* Dummy */
  666.         }
  667.         break;
  668.     } else if ( ch == CAN ) {
  669.             Xmd_msg("Recive Cansel Code !!");
  670.         Soft_Timer(DSP_WAIT);
  671.             break;
  672.         } else if ( ch == SOH || ch == STX ) {
  673.         bufsize = (ch == SOH) ? 128 : 1024;
  674.  
  675.         sectcur = Recive();
  676.             if ( (sectcur + Recive()) != 0xff ) {
  677.                Xmd_msg("PacNo. SUM Error");
  678.         goto ERROR;
  679.         }
  680.             if ( sectcur != 0 && sectcur != (bk & 0xff) ) {
  681.         sprintf((char *)tmp,"PacNo. Err rcv=%4d cal=%4d",sectcur,bk);
  682.                Xmd_msg(tmp);
  683.         goto ERROR;
  684.         }
  685.         if ( Recive_str(tmp,bufsize) != FALSE ) {
  686.                Xmd_msg("Data Read Error");
  687.         goto ERROR;
  688.         }
  689.         for ( crcsum = i = 0 ; i < bufsize ; i++ ) {
  690.                crcsum = (crcsum<<B) ^ crctab[(crcsum>>(W-B)) ^ tmp[i]];
  691.         }
  692.  
  693.         /* CRC */
  694.         k = Recive(); k = (k<<8) | Recive();
  695.         if ( k != crcsum ) {
  696.                 Xmd_msg("CRC Error");
  697.         goto ERROR;
  698.         }
  699.  
  700.  
  701.         if (sectcur == 0) {    /*ヘッダの場合*/
  702.         strcpy(file, (char *)tmp);
  703.         p = tmp + strlen(tmp) + 1;
  704.         sscanf(p, "%ld%lo%o", &filesize, &modtime, &filemode);
  705.  
  706.         Xmd_wind("DownLoad",file);
  707.         Xmd_val(XM_FSIZE,filesize);
  708.         if ( Flaing == FALSE ) {
  709.             Xmd_msg("Y-Modem/1024");
  710.         }else{
  711.             Xmd_msg("Y-Modem-g");
  712.         }
  713.         i = filesize / 1024;
  714.         i += ((filesize - i * 1024)+127) / 128;
  715.             Xmd_val(XM_TBLK,i);
  716.  
  717.             if ( (fp = fopen(file,"wb")) == NULL ) {
  718.             wrtstr("ファイルが作成出来ません",30,1,0x12);
  719.                 send_aux(CAN); send_aux(CAN); send_aux(CAN);
  720.                 Soft_Timer(DSP_WAIT);
  721.                 break;
  722.         }
  723.             fsttime = time(t);
  724.  
  725.         } else {
  726.             if (filesize >= (size + bufsize)) {
  727.                 fwrite(tmp,1,bufsize,fp);
  728.                    size += bufsize;
  729.             }else{
  730.                 fwrite(tmp,1,(filesize - size),fp);
  731.                    size = filesize;
  732.             }
  733.         }
  734.  
  735.         nowtime = time(t);
  736.         if ((nowtime-fsttime) != 0) {
  737.             Xmd_val(XM_RATE,size/(nowtime-fsttime));
  738.         }
  739.         Xmd_val(XM_ERTIME,(nowtime-fsttime));
  740.         Xmd_val(XM_RSIZE,size);
  741.         Xmd_val(XM_RBLK,bk);
  742.         ef = FALSE;
  743.         nk = 0;
  744.         bk ++;
  745.         continue;
  746.     }
  747.  
  748.     sprintf((char *)tmp,"Undefined Code %2X     ",ch);
  749.     Xmd_msg(tmp);
  750.  
  751. ERROR:  Recive_str(tmp,bufsize); /* Dummy */
  752.     ef = TRUE;
  753.     Xmd_val(XM_RETRY,++nk);
  754.         if ( nk >= 5 ) {
  755.             Xmd_msg("ReTry Over Abort !!");
  756.         send_aux(CAN);
  757.         Soft_Timer(DSP_WAIT);
  758.         break;
  759.     }
  760.     }
  761.     fclose(fp);
  762. }
  763.  
  764. /**************************
  765.     Auto Login
  766. **************************/
  767. int    htob(ch)
  768. int     ch;
  769. {
  770.     int     i=0;
  771.  
  772.     if ( ch >= 'a' && ch <= 'z' )
  773.     ch &= 0xDF;
  774.     if ( '0' <= ch && ch <= '9' )
  775.         i = ch - '0';
  776.     else if ( 'A' <= ch && ch <= 'F' )
  777.         i = ch - 'A' + 10;
  778.     return i;
  779. }
  780. int    Wait_recive(tick)
  781. int    tick;
  782. {
  783.     int     len,ch;
  784.     unsigned int st;
  785.     time_t  l,s;
  786.  
  787.     time(&l); l += tick;
  788.     do {
  789.     RSB_Read(port,&len);
  790.         if ( len > 0 ) {
  791.         RSB_Receive(port,&ch,&st);
  792.         BakPut(ch); Dsp_vram(cvram);
  793.         return ch;
  794.     }
  795.     time(&s);
  796.     } while ( kbhit() == 0 && s < l );
  797.     return ERR;
  798. }
  799. void    RS_Sleep(tick)
  800. int    tick;
  801. {
  802.     int     len,ch;
  803.     unsigned int st;
  804.     time_t  l,s;
  805.  
  806.     time(&l); l += tick;
  807.     do {
  808.     RSB_Read(port,&len);
  809.         if ( len > 0 ) {
  810.         while ( len-- > 0 ) {
  811.             RSB_Receive(port,&ch,&st);
  812.             BakPut(ch);
  813.         }
  814.         Dsp_vram(cvram);
  815.     }
  816.     time(&s);
  817.     } while ( kbhit() == 0 && s < l );
  818. }
  819. int    cmpstr(arg)
  820. char    *arg;
  821. {
  822.     int     ch,wt;
  823.     register char    *p;
  824.  
  825.     for ( p = arg,wt = 0 ; isdigit(*p) ; p++ )
  826.     wt = wt * 10 + *p - '0';
  827.     if ( *p == ',' && wt > 0 )
  828.     arg = p + 1;
  829.     else
  830.     wt = 30;
  831.  
  832.     for ( p=arg ; *p != ']' ; ) {
  833.         if ( (ch=Wait_recive(wt)) == ERR )
  834.             return ERR;
  835.         if ( ch == (*p & 0xff) )
  836.             p++;
  837.         else
  838.             p=arg;
  839.     }
  840.     return FALSE;
  841. }
  842. int    cmpmac(arg)
  843. char    *arg;
  844. {
  845.     int     ch,wt;
  846.     register char    *p;
  847.  
  848.     for ( p = arg,wt = 0 ; isdigit(*p) ; p++ )
  849.     wt = wt * 10 + *p - '0';
  850.     if ( *p == ',' && wt > 0 )
  851.     arg = p + 1;
  852.     else
  853.     wt = 5;
  854.  
  855.     for ( p=arg ; *p != '}' ; ) {
  856.         if ( (ch=Wait_recive(wt)) == ERR )
  857.             return ERR;
  858.         if ( ch == (*p & 0xff) )
  859.             p++;
  860.         else
  861.             p=arg;
  862.     }
  863.     return FALSE;
  864. }
  865. int    runlog(arg)
  866. register char    *arg;
  867. {
  868.     int     ch;
  869.     char    *p;
  870.     char    *adr=NULL;
  871.  
  872.     for ( ; *arg != '\0' ; arg++ ) {
  873.         switch(*arg) {
  874.             case '<':
  875.                 send_aux(CR);
  876.                 break;
  877.             case '>':
  878.                 while ( (ch=Wait_recive(30)) != CR ) {
  879.             if ( ch == ERR )
  880.             return ERR;
  881.                     if ( kbhit() != 0 )
  882.                         return TRUE;
  883.                 }
  884.                 break;
  885.             case '$':
  886.                 send_aux( htob(*(++arg)) << 4 | htob(*(++arg)) );
  887.                 break;
  888.             case '\\':
  889.                 send_aux(*(++arg));
  890.                 break;
  891.             case '*':
  892.                 RS_Sleep(2);
  893.                 break;
  894.             case '?':
  895.         adr = arg;
  896.         break;
  897.             case '@':
  898.         adr = NULL;
  899.         break;
  900.             case '[':
  901.                 if ( cmpstr(++arg) != FALSE )
  902.                     return ERR;
  903.                 while ( *arg != ']' ) arg++;
  904.                 break;
  905.             case '{':
  906.         if ( adr == NULL ) {
  907.                     for ( p = arg ; *p != '?' && *p != '\0' ; p++ )
  908.             if ( *p == '?' )
  909.                 adr = p;
  910.             else
  911.                 return ERR;
  912.         }
  913.                 if ( cmpmac(++arg) != FALSE ) {
  914.             if ( kbhit() != 0 )
  915.             return ERR;
  916.             arg = adr;
  917.         } else
  918.                     while ( *arg != '}' ) arg++;
  919.                 break;
  920.             default:
  921.                 send_aux(*arg);
  922.                 break;
  923.         }
  924.         if ( kbhit() != 0 )
  925.             return TRUE;
  926.     }
  927.     return FALSE;
  928. }
  929. char    *Auto_menu[21]={ NULL };
  930. char    *Auto_para[21];
  931.  
  932. void    Auto_log()
  933. {
  934.     static int  no=ERR;
  935.  
  936.     if ( Auto_menu[0] == NULL )
  937.     goto ENDOF;
  938.  
  939.     if ( Sel_menu(Auto_menu,20,2,&no) != FALSE )
  940.     goto ENDOF;
  941.  
  942.     wrtstr(SPCSTR,30,1,0x1F);
  943.     if ( runlog(Auto_para[no]) == ERR )
  944.     wrtstr("オ-トを中断しました",30,1,0x15);
  945.  
  946. ENDOF:
  947.     Dsp_vram(cvram);
  948.     Dsp_free();
  949. }
  950.  
  951. char    *Auto_log_file="WINK.DEF";
  952.  
  953. void    Auto_log_init()
  954. {
  955.     int     i,mx=0;
  956.     FILE    *fp;
  957.     char    *p,*s,*n;
  958.     char    tmp[256];
  959.     char    work[4096];
  960.  
  961.     if ( (p = getenv("WINKAUTO")) != NULL )
  962.     Auto_log_file = p;
  963.  
  964.     if ( (fp = fopen(Auto_log_file,"r")) == NULL )
  965.     return;
  966.  
  967.     while ( (p = fgets(tmp,256,fp)) != NULL ) {
  968.     while ( isspace(*p) ) p++;
  969.     while ( p != NULL && *p == '#' ) {
  970.         for ( s = ++p ; *s != '\n' && *s != '\0' ; s++ );
  971.         *s = '\0';
  972.         if ( (n = Auto_menu[mx] = (char *)malloc(22)) == NULL )
  973.         goto ENDOF;
  974.         for ( i = 0 ; i < 20 && *p != '\0' ; i++ )
  975.         *(n++) = *(p++);
  976.         for ( ; i < 20 ; i++ )
  977.         *(n++) = ' ';
  978.         *n = '\0';
  979.         for ( s = work ; (p = fgets(tmp,256,fp)) != NULL ; ) {
  980.         while ( isspace(*p) ) p++;
  981.         if ( *p == '#' )
  982.             break;
  983.         while ( *p != '\n' && *p != '\0' )
  984.             *(s++) = *(p++);
  985.         }
  986.         *s = '\0';
  987.         if ( (Auto_para[mx] = (char *)malloc(strlen(work)+1)) == NULL )
  988.         goto ENDOF;
  989.         strcpy(Auto_para[mx],work);
  990.         if ( ++mx >= 20 )
  991.             goto ENDOF;
  992.     }
  993.     }
  994. ENDOF:
  995.     fclose(fp);
  996.     Auto_menu[mx] = NULL;
  997. }
  998.